package com.pagepub.core.tag.validator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

public class ValidateMobile extends FormValidateBasic {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8518126384895107544L;

	/**
	 * @param args
	 */
	private String submitvalidate;

	private String rightimage;

	private String rightwordcolor;

	public int doStartTag() throws JspException {
		JspWriter out = (JspWriter) this.pageContext.getOut();
		validatemobile();

		try {
			out.print(validatemobile());

			// System.out.println(str);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return 1;
	}

	public static void main(String[] args) {
		// TODO 自动生成方法存根

	}

	public StringBuffer validatemobile() {
		StringBuffer str = new StringBuffer();
		String emptyelement = "";
		String pluselement = "";
		if (this.submitvalidate.equalsIgnoreCase("true")) {
			emptyelement = "emptyelement(realname);";
			pluselement = "else pluselement(realname);";
		}
		str.append("<script language=\"javascript\">");
		str
				.append("function validatemobile(realvalue,min,max,spanname,realname) {");
		// str.append("alert(realname);");
		str.append("if(realvalue!=\"\")");
		str.append("if(validatemobileformat(realvalue,spanname,realname))");
		str
				.append("validatelengthdisplayrightmessage(realvalue,min,max,spanname,realname);");
		str.append(pluselement);
		str.append("else {");
		this.displayempty();
		str.append(emptyelement);
		str.append("}");
		str.append("}");
		str.append("</script>");
		str.append(this.validatemobileformat());
		str.append(this.returnfunction());
		return str;
	}

	public StringBuffer validatemobileformat() {
		StringBuffer str = new StringBuffer();
		str.append("<script language=\"javascript\">");
		str
				.append("function validatemobileformat(realvalue,spanname,realname){");
		str.append("var flag=0;");
		str.append("var reg=/^(((1[0-9]{1}[0-9]{1}))+\\d{8})$/;");// /(^[0-9]{3,4}\\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\\([0-9]{3,4}\\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;");
		str.append("if(realvalue!=\"\"){");
		str.append(" if (reg.test(realvalue)==false)");
		str.append("			{");
		str.append("				message=\"对不起,\"+realname+\"不是合法号码!\";	");
		str.append("				imgurl=\"<img src=\\\"" + wrongimage + "\\\"/>\";");
		str.append(this.display());
		str.append("return false;");
		str.append("			}");
		str.append("else {");
		str.append(this.displayempty());
		str.append("return true;}");

		str.append("		}");
		str.append("}");
		str.append("</script>");
		return str;
	}

	public StringBuffer returnfunction() {
		StringBuffer string = new StringBuffer();
		ValidateLengthDisplayRightMessage message = new ValidateLengthDisplayRightMessage();
		message.setRightimage(rightimage);
		message.setRightwordcolor(rightwordcolor);
		message.setSubmitvalidate(submitvalidate);
		message.setWordcolor(wordcolor);
		message.setWrongimage(wrongimage);
		string.append(message.validatelengthdisplayrightmessage());
		return string;
	}

	public String getRightimage() {
		return rightimage;
	}

	public void setRightimage(String rightimage) {
		this.rightimage = rightimage;
	}

	public String getRightwordcolor() {
		return rightwordcolor;
	}

	public void setRightwordcolor(String rightwordcolor) {
		this.rightwordcolor = rightwordcolor;
	}

	public String getSubmitvalidate() {
		return submitvalidate;
	}

	public void setSubmitvalidate(String submitvalidate) {
		this.submitvalidate = submitvalidate;
	}
}
